
class ImWebRtc {
	constructor() {
		this.configuration = {}
		this.stream = null;
	}
}

ImWebRtc.prototype.isEnable = function () {
	window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window
		.mozRTCPeerConnection;
	window.RTCSessionDescription = window.RTCSessionDescription || window.webkitRTCSessionDescription || window
		.mozRTCSessionDescription;
	window.RTCIceCandidate = window.RTCIceCandidate || window.webkitRTCIceCandidate || window
		.mozRTCIceCandidate;
	return !!window.RTCPeerConnection;
}

ImWebRtc.prototype.init = function (configuration) {
	this.configuration = configuration;
}

ImWebRtc.prototype.setupPeerConnection = function (callback) {
	this.peerConnection = new RTCPeerConnection(this.configuration);
	this.peerConnection.ontrack = (e) => {
		// The other party's video stream
		callback(e.streams[0]);
	};
}


ImWebRtc.prototype.setStream = function (stream) {
	if (this.stream) {
		this.peerConnection.removeStream(this.stream)
	}
	if (stream) {
		stream.getTracks().forEach((track) => {
			this.peerConnection.addTrack(track, stream);
		});
	}
	this.stream = stream;
}


ImWebRtc.prototype.onIcecandidate = function (callback) {
	this.peerConnection.onicecandidate = (event) => {
		// Tracking candidate information
		if (event.candidate) {
			callback(event.candidate)
		}
	}
}

ImWebRtc.prototype.onStateChange = function (callback) {
	console.log('---------------------webrtc onStateChange')
	// Monitor connection status
	this.peerConnection.oniceconnectionstatechange = (event) => {
		console.log('event', event)
		let state = event.target.iceConnectionState;
		console.log("ICE connection status changes: : " + state)
		callback(state)
	};
}

ImWebRtc.prototype.createOffer = function () {
	return new Promise((resolve, reject) => {
		const offerParam = {};
		offerParam.offerToRecieveAudio = 1;
		offerParam.offerToRecieveVideo = 1;
		// Create local sdp information
		this.peerConnection.createOffer(offerParam).then((offer) => {
			// Set local sdp information
			this.peerConnection.setLocalDescription(offer);
			// Initiate a call request
			resolve(offer)
		}).catch((e) => {
			reject(e)
		})
	});
}


ImWebRtc.prototype.createAnswer = function (offer) {
	return new Promise((resolve, reject) => {
		// Set the remote sdp
		this.setRemoteDescription(offer);
		// Create a local dsp
		const offerParam = {};
		offerParam.offerToRecieveAudio = 1;
		offerParam.offerToRecieveVideo = 1;
		this.peerConnection.createAnswer(offerParam).then((answer) => {
			// Set local sdp information
			this.peerConnection.setLocalDescription(answer);
			// Accept call request
			resolve(answer)
		}).catch((e) => {
			reject(e)
		})
	});
}

ImWebRtc.prototype.setRemoteDescription = function (offer) {
	// Set the other party's sdp information
	this.peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
}

ImWebRtc.prototype.addIceCandidate = function (candidate) {
	// Add the other candidate's information
	this.peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
}

ImWebRtc.prototype.close = function (uid) {
	// Close the RTC connection
	if (this.peerConnection) {
		this.peerConnection.close();
		this.peerConnection.onicecandidate = null;
		this.peerConnection.onaddstream = null;
		this.peerConnection = null;
	}
}

export default ImWebRtc;